package com.sne.airways.basecore.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by IntelliJ IDEA.
 * User: 卞京帅 {bjslogin#gmail.com}
 * Date: 12-11-11
 * Time: 上午11:01
 * SHA 加密
 */
public class PasswordHelper {
    private PasswordHelper(){
    }

    private static final String algorithm = "SHA";

    public static String encodePassword(String password){
        byte[] unencodedPassword = password.getBytes();

        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("it is impossible!");
        }
        md.reset();
        md.update(unencodedPassword);

        byte[] encodedPassword = md.digest();
        StringBuffer buf = new StringBuffer();
        for(byte b : encodedPassword){
            if ((b & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString(b & 0xff, 16));
        }

        return buf.toString();
    }

    public static void main(String[] args){
        System.out.println(encodePassword("000000"));
    }
}
